package com.bs.regsystemapi.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bs.regsystemapi.common.PageResult;
import com.bs.regsystemapi.dao.UserDao;
import com.bs.regsystemapi.entity.Department;
import com.bs.regsystemapi.entity.Duty;
import com.bs.regsystemapi.entity.LoginInfo;
import com.bs.regsystemapi.entity.UserEntity;
import com.bs.regsystemapi.entity.param.DutyParam;
import com.bs.regsystemapi.modal.dto.doctor.QueryDoctorForm;
import com.bs.regsystemapi.modal.dto.doctor.QueryDoctorsForm;
import com.bs.regsystemapi.modal.dto.user.FindPasswordForm;
import com.bs.regsystemapi.modal.dto.user.QueryUserAccountForm;
import com.bs.regsystemapi.modal.dto.user.UserRequestForm;
import com.bs.regsystemapi.modal.vo.TreeData;
import com.bs.regsystemapi.modal.vo.UserInfo;
import com.bs.regsystemapi.modal.vo.doctor.DoctorInfo;
import com.bs.regsystemapi.modal.vo.drug.DrugLabelInfo;
import com.bs.regsystemapi.modal.vo.drug.DrugTypeInfo;
import com.bs.regsystemapi.modal.vo.user.User;
import com.bs.regsystemapi.modal.vo.user.UserAccountInfo;
import com.bs.regsystemapi.service.DepartmentService;
import com.bs.regsystemapi.service.DutyService;
import com.bs.regsystemapi.service.LoginInfoService;
import com.bs.regsystemapi.service.UserService;
import com.bs.regsystemapi.utils.ManagePageResult;
import com.bs.regsystemapi.utils.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wybusy.EasyHttp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @author ning
 * @DATA 2023/01/10
 */
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {
    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private LoginInfoService loginInfoService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private DutyService dutyService;

    @Override
    public String getUserType(String userNo) {
        String userType = this.baseMapper.getUserType(userNo);
        return userType;
    }

    @Override
    public User getUserInfoByLogin(UserRequestForm form) {
        User user = this.baseMapper.getUserInfoByLogin(form);
        return user;
    }

    @Override
    public int findUserByEmail(String userTel) {
        int num = this.baseMapper.findUserByEmail(userTel);
        return num;
    }

    @Override
    public void updatePasswordByEmail(FindPasswordForm form) {
        this.baseMapper.updatePasswordByEmail(form);
    }

    @Override
    public void updateUser(UserEntity user) {
        this.baseMapper.updateUser(user);
    }

    @Override
    public UserEntity getUserInfo(String userNo) {
        UserEntity user = this.baseMapper.getUserInfo(userNo);
        return user;
    }

    @Override
    public UserInfo getBaseUserInfo(String userNo) {
        return this.baseMapper.getBaseUserInfo(userNo);
    }

    @Override
    public ManagePageResult getDoctorList(QueryDoctorForm form) {
        PageHelper.startPage(form.getPageNum(), form.getPageSize());
        List<DoctorInfo> doctorList = this.baseMapper.getDoctorList(form);
        PageInfo<DoctorInfo> doctorInfoPageInfo = new PageInfo<>(doctorList);
        List<DoctorInfo> list = doctorInfoPageInfo.getList();
        for(DoctorInfo doctorInfo : list) {
            List<LoginInfo> loginOnline = loginInfoService.getLoginOnline(doctorInfo.getUserNo());
            if(loginOnline != null && loginOnline.size() > 0) {
                doctorInfo.setLoginOnline(1);
            } else {
                doctorInfo.setLoginOnline(0);
            }
        }
        return ManagePageResult.getPageResult(doctorInfoPageInfo);
    }

    @Override
    public DoctorInfo getDoctorInfo(String userNo) {
        DoctorInfo doctorInfo = this.baseMapper.getDoctorInfo(userNo);
        if(!StringUtils.isEmpty(doctorInfo.getUserDepartment())) {
            Department departmentInfo = departmentService.getDepartmentInfo(doctorInfo.getUserDepartment());
            if(departmentInfo != null) {
                doctorInfo.setDepartment(departmentInfo.getDepartment());
                doctorInfo.setSecondDepartment(departmentInfo.getSecondDepartment());
            }
        }
        return doctorInfo;
    }

    @Override
    public List<DoctorInfo> getDoctorsInfo(QueryDoctorsForm form) {
        List<DoctorInfo> doctorsInfo = this.baseMapper.getDoctorsInfo(form);
        return doctorsInfo;
    }

    @Override
    public boolean deleteDoctor(String userNo) {
        int i = this.baseMapper.deleteDoctor(userNo);
        return i > 0;
    }

    @Override
    public boolean recoverDoctor(String userNo) {
        int i = this.baseMapper.recoverDoctor(userNo);
        return i > 0;
    }

    @Override
    public List<DoctorInfo> getDeleteList() {
        List<DoctorInfo> deleteList = this.baseMapper.getDeleteList();
        return deleteList;
    }

    @Override
    public List<TreeData> getTotalUser() {
        List<TreeData> dataList = new ArrayList<>();
        TreeData admin = new TreeData();
        TreeData doctor = new TreeData();
        // 为admin灌数据
        admin.setValue("admin");
        admin.setLabel("管理员");
        List<UserEntity> allAdmin = this.baseMapper.getAdmin();
        List<TreeData> adminChildren = new ArrayList<>();
        for(UserEntity user : allAdmin) {
            TreeData adminFirst = new TreeData();
            adminFirst.setValue(user.getUserNo());
            adminFirst.setLabel(user.getUserRealName());
            adminChildren.add(adminFirst);
        }
        admin.setChildren(adminChildren);
        // 为doctor灌数据
        doctor.setValue("doctor");
        doctor.setLabel("医生");
        List<DrugTypeInfo> typeInfo = departmentService.getTypeInfo();
        List<TreeData> children1 = new ArrayList<>();
        for(DrugTypeInfo drugTypeInfo : typeInfo) {
            TreeData first = new TreeData();
            first.setValue(drugTypeInfo.getValue());
            first.setLabel(drugTypeInfo.getLabel());
            List<DrugLabelInfo> children = drugTypeInfo.getChildren();
            List<TreeData> children2 = new ArrayList<>();
            for(DrugLabelInfo drugLabelInfo : children) {
                TreeData second = new TreeData();
                second.setValue(drugLabelInfo.getValue());
                second.setLabel(drugLabelInfo.getLabel());
                QueryDoctorsForm form = new QueryDoctorsForm();
                form.setUserDepartment(second.getValue());
                List<DoctorInfo> doctorsInfo = this.getDoctorsInfo(form);
                List<TreeData> children3 = new ArrayList<>();
                for(DoctorInfo doctorInfo : doctorsInfo) {
                    TreeData third = new TreeData();
                    third.setValue(doctorInfo.getUserNo());
                    third.setLabel(doctorInfo.getUserRealName());
                    children3.add(third);
                }
                second.setChildren(children3);
                children2.add(second);
            }
            first.setChildren(children2);
            children1.add(first);
        }
        doctor.setChildren(children1);
        dataList.add(admin);
        dataList.add(doctor);
        return dataList;
    }

    @Override
    public ManagePageResult getAccountInfos(QueryUserAccountForm form) {
        PageHelper.startPage(form.getPageNum(), form.getPageSize());
        List<UserAccountInfo> accountInfo = this.baseMapper.getAccountInfos(form);
        PageInfo<UserAccountInfo> userAccountInfoPageInfo = new PageInfo<>(accountInfo);
        return ManagePageResult.getPageResult(userAccountInfoPageInfo);
    }

    @Override
    public UserAccountInfo getAccountInfo(String infoNo) {
        return this.baseMapper.getAccountInfo(infoNo);
    }

    /**
     * 医生排班
     * @param duty
     * @return
     */
    @Override
    public boolean addDuty(String userNo, DutyParam param) {
        Duty duty = new Duty();
        UserEntity user = (UserEntity) StpUtil.getSession().get("user");

        LambdaQueryWrapper<Duty> lbw = new LambdaQueryWrapper<>();
        lbw.eq(Duty::getDoctorNo,userNo);
        boolean remove = dutyService.remove(lbw);
/*        if (!remove){
            return false;
        }*/

        duty.setDutyNo(UUID.randomUUID().toString());
        duty.setUpdateTime(new Date());
        duty.setUpdateName(user.getUserRealName());
        duty.setDoctorNo(userNo);

        if (param.isMonMorning()){
            duty.setMonMorning("1");
        }
        if (param.isMonAfternoon()){
            duty.setMonAfternoon("1");
        }
        if (param.isMonNight()){
            duty.setMonNight("1");
        }
        if (param.isTueMorning()){
            duty.setTueMorning("1");
        }
        if (param.isTueAfternoon()){
            duty.setTueAfternoon("1");
        }
        if (param.isTueNight()){
            duty.setTueNight("1");
        }
        if (param.isThurMorning()){
            duty.setThurMorning("1");
        }
        if (param.isThurAfternoon()){
            duty.setThurAfternoon("1");
        }
        if (param.isThurNight()){
            duty.setThurNight("1");
        }
        if (param.isWedMorning()){
            duty.setWedMorning("1");
        }
        if (param.isWedAfternoon()){
            duty.setWedAfternoon("1");
        }
        if (param.isWedNight()){
            duty.setWedNight("1");
        }
        if (param.isFriMorning()){
            duty.setFriMorning("1");
        }
        if (param.isFirAfternoon()){
            duty.setFirAfternoon("1");
        }
        if (param.isFirNight()){
            duty.setFirNight("1");
        }
        if (param.isSatMorning()){
            duty.setSatMorning("1");
        }
        if (param.isSatAfternoon()){
            duty.setSatAfternoon("1");
        }
        if (param.isSatNight()){
            duty.setSatNight("1");
        }
        if (param.isSunMorning()){
            duty.setSunMorning("1");
        }
        if (param.isSunAfternoon()){
            duty.setSunAfternoon("1");
        }
        if (param.isSunNight()){
            duty.setSunNight("1");
        }

        return dutyService.save(duty);
    }

    /**
     * app端分页查询医生
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        List<DoctorInfo> dtoList = new ArrayList<>();
        LambdaQueryWrapper<Department> lbw = new LambdaQueryWrapper<>();

        if (StringUtils.isNotEmpty(queryString)){
            lbw.like(Department::getSecondDepartment,queryString);
        }

        List<Department> list = departmentService.list(lbw);
        for (Department department : list) {
            LambdaQueryWrapper<UserEntity> lbw1 = new LambdaQueryWrapper<>();
            lbw1.eq(UserEntity::getUserDepartment,department.getSecondId());
            List<UserEntity> userList = this.list(lbw1);

            for (UserEntity userEntity : userList) {
                DoctorInfo dto = new DoctorInfo();
                BeanUtils.copyProperties(userEntity,dto);
                dto.setDepartment(department.getDepartment());
                dto.setSecondDepartment(department.getSecondDepartment());
                dtoList.add(dto);
            }
        }

        //List<DoctorInfo> doctorList = this.baseMapper.getAppDoctorList(queryString);
        PageInfo<DoctorInfo> doctorInfoPageInfo = new PageInfo<>(dtoList);
        return new PageResult(doctorInfoPageInfo.getTotal(),doctorInfoPageInfo.getList());
    }
}
